<template>
  <div class="select-popup-container">
    <!-- 选择器触发区域 -->
    <div class="selector-trigger" @click="openPopup">
      <div class="input-wrapper">
        <span class="label" :class="{ required: required }">{{ label }}</span>
        <div class="selected-text" :style="{ color: selectedName ? '#000' : '#cccccc' }">
          {{ selectedName || placeholder }}
        </div>
        <div class="address-arrow">
          <text class="gui-icons arrow-icon">&#xe601;</text>
        </div>
      </div>
    </div>

    <!-- 遮罩层 -->
    <div class="popup-mask" v-if="showPopup" @click="closePopup"></div>

    <!-- 弹出选择框 -->
    <div class="popup-container" v-if="showPopup">
      <div class="popup-header">
        <div class="popup-title">{{ title || "请选择供应商" }}</div>
        <div class="close-icon" @click="closePopup">×</div>
      </div>

      <div class="popup-content">
        <div
          v-for="item in options"
          :key="item.id"
          class="option-item"
          :class="{ active: modelValue === item.id }"
          @click="selectItem(item)"
        >
          <div class="option-text">
            <div style="display: flex; align-items: center">
              <gui-image
                :src="
                  item.logo ||
                  item.picture ||
                  'https://cphimg.leyoo888.com/dev/upload/supplier/2025-05-06/1746528989179.png'
                "
                :width="120"
                :height="120"
                mode="fill"
              />
              <div
                style="
                  margin-left: 20rpx;
                  display: flex;
                  flex-direction: column;
                  justify-content: space-around;
                "
              >
                <div>{{ item.name }}</div>
              </div>
            </div>
          </div>
          <i class="check-icon" v-if="modelValue === item.id">✓</i>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from "vue";

// 定义组件属性
const props = defineProps({
  // 选择器标签
  label: {
    type: String,
    default: "品牌",
  },
  // 是否必选
  required: {
    type: Boolean,
    default: false,
  },
  // 占位提示文字
  placeholder: {
    type: String,
    default: "请选择品牌",
  },
  // 弹窗标题
  title: {
    type: String,
    default: "请选择",
  },
  // 选项数据，格式: [{id: 1, name: '品牌1'}, ...]
  options: {
    type: Array,
    default: () => [],
  },
  // 已选中的id（双向绑定）
  modelValue: {
    type: [String, Number],
    default: "",
  },
});

// 定义事件
const emit = defineEmits(["update:modelValue", "changeSupplier"]);

// 弹窗显示状态
const showPopup = ref(false);

// 计算已选择的名称
const selectedName = computed(() => {
  const selected = props.options.find((item) => item.id === props.modelValue);
  return selected ? selected.name : "";
});

// 选择项目
const selectItem = (item) => {
  emit("update:modelValue", item.id);
  emit("changeSupplier", item);
  closePopup();
};

// 打开弹窗
const openPopup = () => {
  showPopup.value = true;
};

// 关闭弹窗
const closePopup = () => {
  showPopup.value = false;
};
</script>

<style scoped>
.select-popup-container {
  position: relative;
  width: 100%;
  font-size: 28rpx;
}

.selector-trigger {
  width: 100%;
}

.input-wrapper {
  display: flex;
  align-items: center;
  padding: 24rpx 0;
  border-bottom: 1px solid #ebeef5;
  position: relative;
}

.label {
  width: 180rpx;
  font-size: 28rpx;
  color: #333;
}

.required::before {
  content: "*";
  color: #f56c6c;
  margin-right: 4rpx;
}

.selected-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: end;
  font-weight: bolder;
}
.arrow-down {
  display: inline-block;
  width: 16rpx;
  height: 16rpx;
  border-right: 2rpx solid #999;
  border-bottom: 2rpx solid #999;
  transform: rotate(45deg);
}

/* 弹出层样式 */
.popup-mask {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
}

.popup-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #fff;
  border-radius: 20rpx 20rpx 0 0;
  z-index: 1000;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1px solid #ebeef5;
}

.popup-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.close-icon {
  font-size: 40rpx;
  color: #999;
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.popup-content {
  max-height: 60vh;
  overflow-y: auto;
  padding: 0 30rpx;
}

.option-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1px solid #ebeef5;
}

.option-item.active {
  color: #1989fa;
}

.option-text {
  font-size: 28rpx;
}

.checkbox {
  width: 40rpx;
  height: 40rpx;
  border: 1px solid #dcdfe6;
  border-radius: 4rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.checkbox.checked {
  background-color: #1989fa;
  border-color: #1989fa;
  color: #fff;
}

.check-icon {
  font-size: 40rpx;
  font-weight: bold;
}
.address-arrow {
  width: 40rpx;
  text-align: right;
}

.arrow-icon {
  color: #ccc;
  font-size: 32rpx;
}
</style>
